package com.tjsinfo.tjsoa.check.aciton;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.excel.util.manager.ReportManager;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.tjsinfo.tjsoa.check.bo.TCheckManage;
import com.tjsinfo.tjsoa.check.vo.TCheckEmployee;
import com.tjsinfo.tjsoa.check.vo.TCheckLead;
import com.tjsinfo.tjsoa.check.vo.TCheckMain;
import com.tjsinfo.tjsoa.check.vo.TCheckMessage;
import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.User;

public class CheckAction  extends DispatchAction{
	private TCheckManage checkManage;

	public TCheckManage getCheckManage() {
		return checkManage;
	}

	public void setCheckManage(TCheckManage checkManage) {
		this.checkManage = checkManage;
	}
	
	/**
	 * 总考核列表
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward mainCheckList(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		List<TCheckMain> checkmainlist = checkManage.selectTCheckMain();
		arg2.setAttribute("checkmainlist", checkmainlist);
		return arg0.findForward("checkmainlist");
	}
	
	/**
	 * 导入领导班子考核表数据
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward addLeadCheck(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		//提交上传附件
		User user = SessionUser.getUser(arg2);
		String fromFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
    	String path = arg2.getSession().getServletContext().getRealPath("/");
    	String xmlURL = 	path+"\\check\\leadexcel.xml";
    	String realpath = arg2.getSession().getServletContext().getRealPath("/");
    	String filePath = realpath+"\\check\\";
    	List<Files> files = DealString.fileCopyTofile(arg2, fromFilePath, filePath);
    	TCheckMain tCheckMain = new TCheckMain();
    	tCheckMain.setTitle(arg2.getParameter("title"));
    	tCheckMain.setType("领导班子");
    	tCheckMain.setCreatedate(new Date());
    	tCheckMain.setUserid(user.getUser_id());
    	for (Files file : files) {
        	checkManage.loadCheckLead(xmlURL, file.getToFilePath(),tCheckMain);

		}
    	List<TCheckMain> checkmainlist = checkManage.selectTCheckMain();
		arg2.setAttribute("checkmainlist", checkmainlist);
		return arg0.findForward("checkmainlist");
	}
	
	
	/**
	 * 导入干部考核表数据
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward addEmployeeCheck(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		
		User user = SessionUser.getUser(arg2);
		String fromFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
    	String path = arg2.getSession().getServletContext().getRealPath("/");
    	String xmlURL = 	path+"\\check\\employeeexcel.xml";
    	String realpath = arg2.getSession().getServletContext().getRealPath("/");
    	String filePath = realpath+"\\check\\";
    	List<Files> files = DealString.fileCopyTofile(arg2, fromFilePath, filePath);
    	TCheckMain tCheckMain = new TCheckMain();
    	tCheckMain.setTitle(arg2.getParameter("title"));
    	tCheckMain.setType("干部");
    	tCheckMain.setCreatedate(new Date());
    	tCheckMain.setUserid(user.getUser_id());
    	for (Files file : files) {
    		checkManage.loadCheckEmployee(xmlURL, file.getToFilePath(),tCheckMain);

		}
    	
    	List<TCheckMain> checkmainlist = checkManage.selectTCheckMain();
		arg2.setAttribute("checkmainlist", checkmainlist);
		return arg0.findForward("checkmainlist");
	}
	
	
	/**
	 * 领导班子数据汇总
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward checkleadmain(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Integer id = Integer.parseInt(arg2.getParameter("id")); 
		List<TCheckLead> checkLeads = checkManage.checkleadmain(id);
		arg2.setAttribute("checkleadmainList", checkLeads);
		arg2.setAttribute("id", id);
		return arg0.findForward("checkleadmain");
	}
	
	/**
	 * 干部数据汇总
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward checkEmployeemain(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Integer id = Integer.parseInt(arg2.getParameter("id")); 
		List<TCheckEmployee> checkEmployees = checkManage.checkemployeemain(id);
		arg2.setAttribute("checkEmployeemainList", checkEmployees);
		arg2.setAttribute("id", id);
		return arg0.findForward("checkEmployeemain");
	}
	
	
	/**
	 * 领导班子数据验证
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward varlead(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		String fromFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
    	String path = arg2.getSession().getServletContext().getRealPath("/");
		String filename = arg2.getParameter("filename");
		fromFilePath += filename;
    	String xmlURL = 	path+"\\check\\leadexcel.xml";
		ReportManager rm = new ReportManager();
		List list  =  rm.doReadBody(fromFilePath, xmlURL);
		List<TCheckLead> checkLeads = checkManage.buildCheckLead(list);
		List<TCheckMessage> tCheckMessages = checkManage.leadListVar(checkLeads);
		Gson gson = new Gson();
		String json = gson.toJson(tCheckMessages);
		arg3.getWriter().print(json);
		return null;
	}
	
	/**
	 * 领导干部数据验证
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward varEmployee(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		String fromFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
    	String path = arg2.getSession().getServletContext().getRealPath("/");
    	String filename = arg2.getParameter("filename");
		fromFilePath += filename;
    	String xmlURL = 	path+"\\check\\employeeexcel.xml";
		ReportManager rm = new ReportManager();
		List list  =  rm.doReadBody(fromFilePath, xmlURL);
		List<TCheckEmployee> checkEmployees = checkManage.buildCheckEmployee(list);
		List<TCheckMessage> tCheckMessages = checkManage.employeeListVar(checkEmployees); 
		Gson gson = new Gson();
		String json = gson.toJson(tCheckMessages);
		arg3.getWriter().print(json);
		return null;
	}
	
	/**
	 * 领导班子汇总数据导出
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward leadDataOut(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Integer id = Integer.parseInt(arg2.getParameter("id")); 
		List<TCheckLead> checkLeads = checkManage.checkleadmain(id);
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("Tap1", checkLeads);
		Gson gson = new Gson();
		String jsonString = gson.toJson(valueMap); 
		String excelpath = arg2.getSession().getServletContext().getRealPath("");
		String uuname = "领导班子考核评测表.xls";
		String excelURL = 	excelpath+"\\check\\banzitemp.xls";
		String xmlURL = 	excelpath+"\\check\\leadexcelout.xml";
		String saveURL = 	excelpath+"\\check\\"+uuname;
		ReportManager rm = new ReportManager();
		rm.doReport(excelURL, xmlURL, jsonString, saveURL);
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("fileurl", uuname);
		arg3.getWriter().print(jsonObject);
		return null;
	}
	
	/**
	 * 领导干部汇总数据导出
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward employeeDataOut(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Integer id = Integer.parseInt(arg2.getParameter("id")); 
		List<TCheckEmployee> checkEmployees = checkManage.checkemployeemain(id);
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("Tap1", checkEmployees);
		Gson gson = new Gson();
		String jsonString = gson.toJson(valueMap); 
		String excelpath = arg2.getSession().getServletContext().getRealPath("");
		String uuname = "领导干部考核评测表.xls";
		String excelURL = 	excelpath+"\\check\\employeetemp.xls";
		String xmlURL = 	excelpath+"\\check\\employeeexcelout.xml";
		String saveURL = 	excelpath+"\\check\\"+uuname;
		ReportManager rm = new ReportManager();
		rm.doReport(excelURL, xmlURL, jsonString, saveURL);
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("fileurl", uuname);
		arg3.getWriter().print(jsonObject);
		return null;
	}
	
	/**
	 * 删除考核评审表
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteleadmain(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Integer id = Integer.parseInt(arg2.getParameter("id")); 
		checkManage.deleteMain(id);
		List<TCheckMain> checkmainlist = checkManage.selectTCheckMain();
		arg2.setAttribute("checkmainlist", checkmainlist);
		return arg0.findForward("checkmainlist");
	}
	
	
}
